package com.wu.leetcode.leetcode557;

/**
 * Leetcode557-反转字符串中的单词 III
 * 输入: "Let's take LeetCode contest"
 * 输出: "s'teL ekat edoCteeL tsetnoc"
 */
public class ReverseString {
	public static String reverseWords(String s) {
		/*char[] ch = s.toCharArray();
		int n = 0;
		//定义在全局
		Stack stack=new Stack();
		for(int i = 0;i < s.length();i++) {
			//遇见空格则停下，弹出单词依次存入数组
			if(ch[i] !=' ')
			{
				stack.push(ch[i]);
			}else {
				//当栈不为空时，一直弹出，即为一个完整的单词
				while (!stack.empty()) {
					ch[n++] = (char)stack.pop();
				}
				ch[n++] = ' ';
			}
		}
		//因为最后一个单词后面没有空格，所以需要执行最后一次弹出
		while (!stack.empty()){
			ch[n++] = (char) stack.pop();
		}
		return new String(ch);*/
		String[] s1 = s.split(" ");
		StringBuilder res = new StringBuilder();
		for (int i = 0; i < s1.length; i++) {
			StringBuilder builder = new StringBuilder(s1[i]);
			res.append(builder.reverse()+ " ");

		}
		return res.toString().trim();
	}
}
